﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Core;
using System.Xml.Linq;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.PowerPoint;

namespace RMSWebService
{
    public class EncryptFileProvider
    {
        public static readonly string STRATEGY_PATH = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["StrategyPath"]));
        public static readonly string STRATEGY_FILE = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["StrategyFile"]));
        public static readonly string SHARE_USER = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["ShareUser"]));
        public static readonly string SHARE_PWD = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SharePwd"]));

        /// <summary>
        /// 加密Word
        /// </summary>
        /// <param name="filemodel">文件属性</param>
        /// <param name="path">文件路径</param>
        /// <returns>加密结果</returns>
        public static bool EncryptWord(RMSFileModel filemodel, string path)
        {
            var wordClass = new Microsoft.Office.Interop.Word.ApplicationClass();
            Documents documents = wordClass.Documents;
            object obj = filemodel.FSourcePath;
            object missing = Type.Missing;
            object missing2 = Type.Missing;
            object missing3 = Type.Missing;
            object missing4 = Type.Missing;
            object missing5 = Type.Missing;
            object missing6 = Type.Missing;
            object missing7 = Type.Missing;
            object missing8 = Type.Missing;
            object missing9 = Type.Missing;
            object missing10 = Type.Missing;
            object missing11 = Type.Missing;
            object missing12 = Type.Missing;
            object missing13 = Type.Missing;
            object missing14 = Type.Missing;
            object obj2 = Type.Missing;
            Document mydoc = documents.Open(ref obj, ref missing, ref missing2, ref missing3, 
                ref missing4, ref missing5, ref missing6, ref missing7, ref missing8, 
                ref missing9, ref missing10, ref missing11, ref missing12, ref missing13, ref missing14, ref obj2);
            Thread.Sleep(5000);
            bool result;
            try
            {
                DateTime exprieDateTime = new DateTime(2027, 05, 19);
                if (mydoc != null)
                {
                    mydoc.Activate();
                    var irmPremission = mydoc.Permission;
                    bool enabled = irmPremission.Enabled;
                    if (enabled)
                    {
                        filemodel.FErrorMessage = "加密已加密的Word时错误";
                        filemodel.FContent = "当前文档已经加密，操作失败!";
                        return false;
                    }
                    mydoc.Application.Visible = false;
                    irmPremission.Enabled = true;
                    bool flag2 = filemodel.UserRights != null && filemodel.UserRights.Count > 0;
                    if (flag2)
                    {
                        foreach (UserRight UserRight in filemodel.UserRights)
                        {

                            bool flag3 = UserRight.RightName == "OWNER";
                            if (flag3)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionFullControl, exprieDateTime);
                            }
                            bool flag4 = UserRight.RightName == "PRINT";
                            if (flag4)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionPrint, exprieDateTime);
                            }
                            bool flag5 = UserRight.RightName == "VIEW";
                            if (flag5)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionView, exprieDateTime);
                            }
                            bool flag6 = UserRight.RightName == "EXPORT";
                            if (flag6)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionSave, exprieDateTime);
                            }
                            bool flag7 = UserRight.RightName == "EDIT";
                            if (flag7)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionEdit, exprieDateTime);
                            }

                        }
                    }
                    else
                    {
                        irmPremission.Add("Everyone", MsoPermission.msoPermissionEdit, exprieDateTime);
                    }
                }

                string tempPath = path + filemodel.FTitle;
                _Document document = mydoc;
                obj2 = tempPath;
                missing14 = Type.Missing;
                missing13 = Type.Missing;
                missing12 = Type.Missing;
                missing11 = Type.Missing;
                missing10 = Type.Missing;
                missing9 = Type.Missing;
                missing8 = Type.Missing;
                missing7 = Type.Missing;
                missing6 = Type.Missing;
                missing5 = Type.Missing;
                missing4 = Type.Missing;
                missing3 = Type.Missing;
                missing2 = Type.Missing;
                missing = Type.Missing;
                obj = Type.Missing;
                document.SaveAs(ref obj2, ref missing14, ref missing13, ref missing12, ref missing11, ref missing10, ref missing9, ref missing8, ref missing7, ref missing6, ref missing5, ref missing4, ref missing3, ref missing2, ref missing, ref obj);
                filemodel.FRMSPath = tempPath;
                filemodel.FContent = "加密成功";
                result = true;
            }
            catch (Exception ex)
            {
                filemodel.FErrorMessage = "加密Word时错误:" + ex.ToString();
                filemodel.FContent = ex.ToString();
                result = false;
            }
            finally
            {
                _Document document2 = mydoc;
                obj = Type.Missing;
                missing = Type.Missing;
                missing2 = Type.Missing;
                document2.Close(ref obj, ref missing, ref missing2);
                Microsoft.Office.Interop.Word.ApplicationClass applicationClass = wordClass;
                missing2 = Type.Missing;
                missing = Type.Missing;
                obj = Type.Missing;
                applicationClass.Quit(ref missing2, ref missing, ref obj);
            }
            return result;
        }
        /// <summary>
        /// 解密Word
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns>解密结果</returns>
        public static bool DecryptWord(RMSFileModel filemodel, string path)
        {
            Microsoft.Office.Interop.Word.ApplicationClass wordClass = new Microsoft.Office.Interop.Word.ApplicationClass();
            Documents documents = wordClass.Documents;
            object obj = filemodel.FSourcePath;
            object missing = Type.Missing;
            object missing2 = Type.Missing;
            object missing3 = Type.Missing;
            object missing4 = Type.Missing;
            object missing5 = Type.Missing;
            object missing6 = Type.Missing;
            object missing7 = Type.Missing;
            object missing8 = Type.Missing;
            object missing9 = Type.Missing;
            object missing10 = Type.Missing;
            object missing11 = Type.Missing;
            object missing12 = Type.Missing;
            object missing13 = Type.Missing;
            object missing14 = Type.Missing;
            object obj2 = Type.Missing;
            Document mydoc = documents.Open(ref obj, ref missing, ref missing2, ref missing3, ref missing4, ref missing5, ref missing6, ref missing7, ref missing8, ref missing9, ref missing10, ref missing11, ref missing12, ref missing13, ref missing14, ref obj2);
            Thread.Sleep(5000);
            bool result;
            try
            {
                DateTime exprieDateTime = new DateTime(2027, 5, 19);
                bool flag = mydoc != null;
                if (flag)
                {
                    mydoc.Activate();
                    Permission irmPremission = mydoc.Permission;
                    bool flag2 = !irmPremission.Enabled;
                    if (flag2)
                    {
                        filemodel.FErrorMessage = "解密已解密的Word时错误";
                        filemodel.FContent = "当前文档已经解密，操作失败!";
                        return false;
                    }
                    mydoc.Application.Visible = false;
                    irmPremission.Enabled = false;
                }
                string tempPath = path + filemodel.FTitle;
                _Document document = mydoc;
                obj2 = tempPath;
                missing14 = Type.Missing;
                missing13 = Type.Missing;
                missing12 = Type.Missing;
                missing11 = Type.Missing;
                missing10 = Type.Missing;
                missing9 = Type.Missing;
                missing8 = Type.Missing;
                missing7 = Type.Missing;
                missing6 = Type.Missing;
                missing5 = Type.Missing;
                missing4 = Type.Missing;
                missing3 = Type.Missing;
                missing2 = Type.Missing;
                missing = Type.Missing;
                obj = Type.Missing;
                document.SaveAs(ref obj2, ref missing14, ref missing13, ref missing12, ref missing11, ref missing10, ref missing9, ref missing8, ref missing7, ref missing6, ref missing5, ref missing4, ref missing3, ref missing2, ref missing, ref obj);
                filemodel.FRMSPath = tempPath;
                filemodel.FContent = "解密成功";
                result = true;
            }
            catch (Exception ex)
            {
                filemodel.FErrorMessage = "解密Word时错误";
                filemodel.FContent = ex.ToString();
                result = false;
            }
            finally
            {
                _Document document2 = mydoc;
                obj = Type.Missing;
                missing = Type.Missing;
                missing2 = Type.Missing;
                document2.Close(ref obj, ref missing, ref missing2);
                Microsoft.Office.Interop.Word.ApplicationClass applicationClass = wordClass;
                missing2 = Type.Missing;
                missing = Type.Missing;
                obj = Type.Missing;
                applicationClass.Quit(ref missing2, ref missing, ref obj);
            }
            return result;
        }

        /// <summary>
        /// 加密Excel
        /// </summary>
        /// <param name="filemodel">文件属性</param>
        /// <param name="path">文件路径</param>
        /// <returns>加密结果</returns>
        public static bool EncryptExcel(RMSFileModel filemodel, string path)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass excelClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Workbook myExecl = excelClass.Workbooks.Open(filemodel.FSourcePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            Thread.Sleep(5000);
            bool result;
            try
            {
                DateTime exprieDateTime = new DateTime(2027, 5, 19);
                bool flag = myExecl != null;
                if (flag)
                {
                    myExecl.Activate();
                    Permission irmPremission = myExecl.Permission;
                    bool enabled = irmPremission.Enabled;
                    if (enabled)
                    {
                        filemodel.FErrorMessage = "加密已加密的Excel时错误";
                        filemodel.FContent = "当前文档已经加密，操作失败!";
                        return false;
                    }
                    myExecl.Application.Visible = false;
                    irmPremission.Enabled = true;
                    bool flag2 = filemodel.UserRights != null && filemodel.UserRights.Count > 0;
                    if (flag2)
                    {
                        foreach (UserRight UserRight in filemodel.UserRights)
                        {
                            bool flag3 = UserRight.RightName == "OWNER";
                            if (flag3)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionFullControl, exprieDateTime);
                            }
                            bool flag4 = UserRight.RightName == "PRINT";
                            if (flag4)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionPrint, exprieDateTime);
                            }
                            bool flag5 = UserRight.RightName == "VIEW";
                            if (flag5)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionView, exprieDateTime);
                            }
                            bool flag6 = UserRight.RightName == "EXPORT";
                            if (flag6)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionSave, exprieDateTime);
                            }
                            bool flag7 = UserRight.RightName == "EDIT";
                            if (flag7)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionEdit, exprieDateTime);
                            }
                        }
                    }
                    else
                    {
                        irmPremission.Add("Everyone", MsoPermission.msoPermissionEdit, exprieDateTime);
                    }
                }
                string tempPath = path + filemodel.FTitle;
                myExecl.SaveAs(tempPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                filemodel.FRMSPath = tempPath;
                filemodel.FContent = "加密成功";
                result = true;
            }
            catch (Exception ex)
            {
                filemodel.FErrorMessage = "加密Excel时错误:" + ex.ToString();
                filemodel.FContent = ex.ToString();
                result = false;
            }
            finally
            {
                myExecl.Close(Type.Missing, Type.Missing, Type.Missing);
                excelClass.Quit();
            }
            return result;
        }

        /// <summary>
        /// 解密Excel
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns>解密结果</returns>
        public static bool DecryptExcel(RMSFileModel filemodel, string path)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass excelClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Workbook myExecl = excelClass.Workbooks.Open(filemodel.FSourcePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            Thread.Sleep(5000);
            bool result;
            try
            {
                DateTime exprieDateTime = new DateTime(2027, 5, 19);
                bool flag = myExecl != null;
                if (flag)
                {
                    myExecl.Activate();
                    Permission irmPremission = myExecl.Permission;
                    bool flag2 = !irmPremission.Enabled;
                    if (flag2)
                    {
                        filemodel.FErrorMessage = "解密已解密的excel时错误";
                        filemodel.FContent = "当前文档已经解密，操作失败!";
                        return false;
                    }
                    myExecl.Application.Visible = false;
                    irmPremission.Enabled = false;
                }
                string tempPath = path + filemodel.FTitle;
                myExecl.SaveAs(tempPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                filemodel.FRMSPath = tempPath;
                filemodel.FContent = "解密成功";
                result = true;
            }
            catch (Exception ex)
            {
                filemodel.FErrorMessage = "解密excel时错误:" + ex.ToString();
                filemodel.FContent = ex.ToString();
                result = false;
            }
            finally
            {
                myExecl.Close(Type.Missing, Type.Missing, Type.Missing);
                excelClass.Quit();
            }
            return result;
        }

        /// <summary>
        /// 加密PPT
        /// </summary>
        /// <param name="filemodel">文件属性</param>
        /// <param name="path">文件路径</param>
        /// <returns>加密结果</returns>
        public static bool EncryptPPT(RMSFileModel filemodel, string path)
        {
            Microsoft.Office.Interop.PowerPoint.ApplicationClass pptClass = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
            Presentation myppt = pptClass.Presentations.Open(filemodel.FSourcePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue);
            Thread.Sleep(5000);
            bool result;
            try
            {
                DateTime exprieDateTime = new DateTime(2027, 5, 19);
                bool flag = myppt != null;
                if (flag)
                {
                    Permission irmPremission = myppt.Permission;
                    bool enabled = irmPremission.Enabled;
                    if (enabled)
                    {
                        filemodel.FErrorMessage = "加密已加密的PPT时错误";
                        filemodel.FContent = "当前文档已经加密，操作失败!";
                        return false;
                    }
                    irmPremission.Enabled = true;
                    bool flag2 = filemodel.UserRights != null && filemodel.UserRights.Count > 0;
                    if (flag2)
                    {
                        foreach (UserRight UserRight in filemodel.UserRights)
                        {
                            bool flag3 = UserRight.RightName == "OWNER";
                            if (flag3)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionFullControl, exprieDateTime);
                            }
                            bool flag4 = UserRight.RightName == "PRINT";
                            if (flag4)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionPrint, exprieDateTime);
                            }
                            bool flag5 = UserRight.RightName == "VIEW";
                            if (flag5)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionView, exprieDateTime);
                            }
                            bool flag6 = UserRight.RightName == "EXPORT";
                            if (flag6)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionSave, exprieDateTime);
                            }
                            bool flag7 = UserRight.RightName == "EDIT";
                            if (flag7)
                            {
                                irmPremission.Add(UserRight.UserName, MsoPermission.msoPermissionEdit, exprieDateTime);
                            }
                        }
                    }
                    else
                    {
                        irmPremission.Add("Everyone", MsoPermission.msoPermissionEdit, exprieDateTime);
                    }
                }
                string tempPath = path + filemodel.FTitle;
                myppt.SaveAs(tempPath, PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTriStateMixed);
                filemodel.FRMSPath = tempPath;
                filemodel.FContent = "加密成功";
                result = true;
            }
            catch (Exception ex)
            {
                filemodel.FErrorMessage = "加密PPT时错误:" + ex.ToString();
                filemodel.FContent = ex.ToString();
                result = false;
            }
            finally
            {
                myppt.Close();
                pptClass.Quit();
            }
            return result;
        }

        /// <summary>
        /// 解密PPT
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns>解密结果</returns>
        public static bool DecryptPPT(RMSFileModel filemodel, string path)
        {
            Microsoft.Office.Interop.PowerPoint.ApplicationClass pptClass = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
            Presentation myppt = pptClass.Presentations.Open(filemodel.FSourcePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue);
            Thread.Sleep(5000);
            bool result;
            try
            {
                DateTime exprieDateTime = new DateTime(2027, 5, 19);
                bool flag = myppt != null;
                if (flag)
                {
                    Permission irmPremission = myppt.Permission;
                    bool flag2 = !irmPremission.Enabled;
                    if (flag2)
                    {
                        filemodel.FErrorMessage = "解密已解密的ppt时错误";
                        filemodel.FContent = "当前文档已经解密，操作失败!";
                        return false;
                    }
                    irmPremission.Enabled = false;
                }
                string tempPath = path + filemodel.FTitle;
                myppt.SaveAs(tempPath, PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTriStateMixed);
                filemodel.FRMSPath = tempPath;
                filemodel.FContent = "解密成功";
                result = true;
            }
            catch (Exception ex)
            {
                filemodel.FErrorMessage = "解密ppt时错误:" + ex.ToString();
                filemodel.FContent = ex.ToString();
                result = false;
            }
            finally
            {
                myppt.Close();
                pptClass.Quit();
            }
            return result;
        }

        /// <summary>
        /// 清理Office进程
        /// </summary>
        public static void KillOfficeProcess()
        {
            try
            {
                Process[] proWord = Process.GetProcessesByName("WinWord");
                foreach (Process word in proWord)
                {
                    word.Kill();
                }
                Process[] proExcel = Process.GetProcessesByName("Excel");
                foreach (Process excel in proExcel)
                {
                    excel.Kill();
                }
                Process[] proPPT = Process.GetProcessesByName("POWERPNT");
                foreach (Process ppt in proPPT)
                {
                    ppt.Kill();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Object判断并转为Email（String格式）
        /// </summary>
        /// <param name="o">Object</param>
        /// <returns>Email</returns>
        public static string ObjIsEmail(object o)
        {
            string email = EncryptFileProvider.ObjToStr(o);
            bool flag = !string.IsNullOrEmpty(email);
            string result;
            if (flag)
            {
                bool flag2 = email.IndexOf("@") > -1;
                if (flag2)
                {
                    result = email;
                }
                else
                {
                    result = "";
                }
            }
            else
            {
                result = "";
            }
            return result;
        }

        /// <summary>
        /// Object转String
        /// </summary>
        /// <param name="o">Object</param>
        /// <returns>String</returns>
        public static string ObjToStr(object o)
        {
            bool flag = o == null;
            string result;
            if (flag)
            {
                result = "";
            }
            else
            {
                result = o.ToString();
            }
            return result;
        }

        /// <summary>
        /// 获取用户权限策略
        /// </summary>
        /// <returns>List</returns>
        public static List<UserRight> GetUserRights() {
            bool status = EncryptFileProvider.connectState(EncryptFileProvider.STRATEGY_PATH, EncryptFileProvider.SHARE_USER, EncryptFileProvider.SHARE_PWD);
            List<UserRight> userRights = new List<UserRight>();
            bool flag = status;
            if (flag)
            {
                XElement xe = XElement.Load(EncryptFileProvider.STRATEGY_PATH + EncryptFileProvider.STRATEGY_FILE);
                IEnumerable<XElement> rightsElement = from ele in xe.Elements("BODY").Elements("WORK").Elements("RIGHTSGROUP").Elements("RIGHTSLIST")
                                                      select ele;
                IEnumerable<XElement> rightElements = from ele in rightsElement.Elements("RIGHT")
                                                      select ele;
                bool flag2 = rightElements.LongCount<XElement>() != 0L;
                if (flag2)
                {
                    foreach (XElement rightElement in rightElements)
                    {
                        bool flag3 = rightElement.Attribute("name").Value == "OWNER";
                        if (flag3)
                        {
                            IEnumerable<XElement> principals = from ele in rightElement.Elements("CONDITIONLIST").Elements("ACCESS").Elements("PRINCIPAL")
                                                               select ele;
                            foreach (XElement principal in principals)
                            {
                                bool flag4 = principal.Element("OBJECT").Element("ID").Attribute("type").Value != "Internal";
                                if (flag4)
                                {
                                    userRights.Add(new UserRight
                                    {
                                        UserName = principal.Element("OBJECT").Element("NAME").Value,
                                        RightName = "OWNER"
                                    });
                                }
                            }
                        }
                    }
                }
                IEnumerable<XElement> viewElement = from ele in rightsElement.Elements("VIEW")
                                                    select ele;
                bool flag5 = viewElement.LongCount<XElement>() != 0L;
                if (flag5)
                {
                    IEnumerable<XElement> principals2 = from ele in viewElement.Elements("CONDITIONLIST").Elements("ACCESS").Elements("PRINCIPAL")
                                                        select ele;
                    foreach (XElement principal2 in principals2)
                    {
                        userRights.Add(new UserRight
                        {
                            UserName = principal2.Element("OBJECT").Element("NAME").Value,
                            RightName = "VIEW"
                        });
                    }
                }
                IEnumerable<XElement> editElement = from ele in rightsElement.Elements("EDIT")
                                                    select ele;
                bool flag6 = editElement.LongCount<XElement>() != 0L;
                if (flag6)
                {
                    IEnumerable<XElement> principals3 = from ele in viewElement.Elements("CONDITIONLIST").Elements("ACCESS").Elements("PRINCIPAL")
                                                        select ele;
                    foreach (XElement principal3 in principals3)
                    {
                        userRights.Add(new UserRight
                        {
                            UserName = principal3.Element("OBJECT").Element("NAME").Value,
                            RightName = "EDIT"
                        });
                    }
                }
                IEnumerable<XElement> printElement = from ele in rightsElement.Elements("PRINT")
                                                     select ele;
                bool flag7 = printElement.LongCount<XElement>() != 0L;
                if (flag7)
                {
                    IEnumerable<XElement> principals4 = from ele in viewElement.Elements("CONDITIONLIST").Elements("ACCESS").Elements("PRINCIPAL")
                                                        select ele;
                    foreach (XElement principal4 in principals4)
                    {
                        userRights.Add(new UserRight
                        {
                            UserName = principal4.Element("OBJECT").Element("NAME").Value,
                            RightName = "PRINT"
                        });
                    }
                }
                IEnumerable<XElement> exportElement = from ele in rightsElement.Elements("EXPORT")
                                                      select ele;
                bool flag8 = exportElement.LongCount<XElement>() != 0L;
                if (flag8)
                {
                    IEnumerable<XElement> principals5 = from ele in viewElement.Elements("CONDITIONLIST").Elements("ACCESS").Elements("PRINCIPAL")
                                                        select ele;
                    foreach (XElement principal5 in principals5)
                    {
                        userRights.Add(new UserRight
                        {
                            UserName = principal5.Element("OBJECT").Element("NAME").Value,
                            RightName = "EXPORT"
                        });
                    }
                }
                return userRights;
            }
            throw new Exception("无法连接共享网络：" + EncryptFileProvider.STRATEGY_PATH);
        }

        /// <summary>
        /// 连接远程共享文件夹
        /// </summary>
        /// <param name="path">远程共享文件夹的路径</param>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        /// <returns></returns>
        public static bool connectState(string path, string userName, string passWord)
        {
            bool Flag = false;
            Process proc = new Process();
            try
            {
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                string clearLine = "net use * /del /y";
                string dosLine = ("net use " + path + " " + passWord + "/User:" + userName) ?? "";
                proc.StandardInput.WriteLine(clearLine);
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (!proc.HasExited)
                {
                    proc.WaitForExit(1000);
                }
                string errormsg = proc.StandardError.ReadToEnd();
                proc.StandardError.Close();
                bool flag = string.IsNullOrEmpty(errormsg);
                if (!flag)
                {
                    throw new Exception("执行共享网络连接命令失败：" + errormsg + "；参数：" + dosLine);
                }
                Flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }

    }
}
